package com.zcx.merchantorder.util;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * @Description: toast管理工具
 * @Author: zcx
 * @CreateDate: 2019/3/2 18:34
 */
public class ToastUtil {

    private static Application application;
    private static boolean isShow = true;//默认显示
    private static Toast mToast = null;//全局唯一的Toast

    public ToastUtil() {
    }

    public static void init(Application application) {
        ToastUtil.application = application;
    }

    /**
    *全局控制是否显示Toast
     * @date: 2019/3/2 18:38
     * @author: zcx
     * @param isShow
     * @return void
     */
    public static void isToastShow(boolean isShow)  {
        isShow = isShow;
    }

    /**
    *取消Toast显示
     * @date: 2019/3/2 18:39
     * @author: zcx
     * @param 
     * @return void
     */
    public static void toastCancel() {
        if(isShow && mToast != null){
            mToast.cancel();
        }
    }

    /**
    *短时间显示Toast
     * @date: 2019/3/2 18:42
     * @author: zcx
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @return void
     */
    public static void toastShowShort(int resId) {
        if (isShow){
            if (mToast == null) {
                mToast = Toast.makeText(application, resId, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(resId);
            }
            mToast.show();
        }
    }

    /**
    *长时间显示Toast
     * @date: 2019/3/2 18:43
     * @author: zcx
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @return void
     */
    public static void toastShowLong(int resId) {
        if (isShow){
            if (mToast == null) {
                mToast = Toast.makeText(application, resId, Toast.LENGTH_LONG);
            } else {
                mToast.setText(resId);
            }
            mToast.show();
        }
    }

    /**
    * 自定义显示Toast时间
     * @date: 2019/3/2 18:46
     * @author: zcx
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @param duration 单位:毫秒
     * @return void
     */
    public static void show(int resId, int duration) {
        if (isShow){
            if (mToast == null) {
                mToast = Toast.makeText(application, resId, duration);
            } else {
                mToast.setText(resId);
            }
            mToast.show();
        }
    }

    /**
    *自定义Toast的View
     * @date: 2019/3/2 18:48
     * @author: zcx
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @param duration 单位:毫秒
     * @param view 显示自己的View
     * @return void
     */
    public static void customToastView(int resId, int duration,View view) {
        if (isShow){
            if (mToast == null) {
                mToast = Toast.makeText(application, resId, duration);
            } else {
                mToast.setText(resId);
            }
            if(view != null){
                mToast.setView(view);
            }
            mToast.show();
        }
    }

    /**
    *自定义Toast的位置
     * @date: 2019/3/2 18:50
     * @author: zcx
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @param duration 单位:毫秒
     * @param gravity 控制显示的位置如:Gravity.CENTER,Gravity.LEFT等
     * @param xOffset x轴偏移量
     * @param yOffset y轴偏移量
     * @return void
     */
    public static void customToastGravity(int resId, int duration,int gravity, int xOffset, int yOffset) {
        if (isShow){
            if (mToast == null) {
                mToast = Toast.makeText(application, resId, duration);
            } else {
                mToast.setText(resId);
            }
            mToast.setGravity(gravity, xOffset, yOffset);
            mToast.show();
        }
    }

    /**
    *自定义带图片和文字的Toast，最终的效果就是上面是图片，下面是文字
     * @date: 2019/3/2 18:54
     * @author: zcx
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @param iconResId 图片的资源id,如:R.drawable.icon
     * @param duration  单位:毫秒
     * @param gravity 控制显示的位置如:Gravity.CENTER,Gravity.LEFT等
     * @param xOffset x轴偏移量
     * @param yOffset y轴偏移量
     * @return void
     */
    public static void showToastWithImageAndText(int resId, int iconResId,int duration,int gravity, int xOffset, int yOffset) {
        if (isShow){
            if (mToast == null) {
                mToast = Toast.makeText(application, resId, duration);
            } else {
                mToast.setText(resId);
            }
            mToast.setGravity(gravity, xOffset, yOffset);
            LinearLayout toastView = (LinearLayout) mToast.getView();
            ImageView imageView = new ImageView(application);
            imageView.setImageResource(iconResId);
            toastView.addView(imageView, 0);
            mToast.show();
        }
    }

    /**
    *
     * @date: 2019/3/2 18:57
     * @author: zcx
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @param duration 单位:毫秒
     * @param view
     * @param isGravity true,表示后面的三个布局参数生效,false,表示不生效
     * @param gravity 控制显示的位置如:Gravity.CENTER,Gravity.LEFT等
     * @param xOffset x轴偏移量
     * @param yOffset y轴偏移量
     * @param isMargin isMargin true,表示后面的两个参数生效，false,表示不生效
     * @param horizontalMargin 水平
     * @param verticalMargin 垂直
     * @return void
     */
    public static void customToastAll(int resId, int duration,View view,boolean isGravity,int gravity, int xOffset, int yOffset,boolean isMargin,float horizontalMargin, float verticalMargin) {
        if (isShow){
            if (mToast == null) {
                mToast = Toast.makeText(application, resId, duration);
            } else {
                mToast.setText(resId);
            }
            if(view != null){
                mToast.setView(view);
            }
            if(isMargin){
                mToast.setMargin(horizontalMargin, verticalMargin);
            }
            if(isGravity){
                mToast.setGravity(gravity, xOffset, yOffset);
            }
            mToast.show();
        }
    }


}
